<?php

namespace app\data\controller;

use app\data\controller\activity\Takes;
use app\data\controller\base\Publics;
use app\data\controller\merchant\Cash;
use think\Db;
use think\Log;

/**
 * 定时
 * Class
 * @package app\data\controller
 */
class Timer
{

	/**
	 * 商户返现
	 */
	public function machant_cash()
	{
		try {
			$file = Cash::instance()->file_path('cash');
			if (!file_exists($file)) {
				Cash::instance()->temyStore();
			}
			$pags = unserialize(file_get_contents($file));
			if ($pags) {
				if ($pags['page'] == $pags["total"]) {
					Cash::instance()->clearTable();
					Cash::instance()->temyStore();
				}
				$upPage = unserialize(file_get_contents($file));
				$p = $upPage["page"] + 1;

				$offset = ($p - 1) * $pags["limit"];
				$ret = Db::name(Publics::instance()->tab()->mer_cash_temy)->limit((int)$offset, (int)$upPage["limit"])->select();
				if ($ret) {
					foreach ($ret as $v) {
						Cash::instance()->cashCalc($v["uid"]);
					}
					$upPage["page"] = $p;
					file_put_contents($file, serialize($upPage));
					echo "商户返现 - 运行正常...................！！！";
				} else {
					throw new \Exception("暂无消息运行...................！！！");
				}
			} else {
				throw new \Exception("翻页信息有异常...................！！！");
			}
		} catch (\Exception $e) {
			echo $e->getMessage();
		}
	}

	/**
	 * 新人礼 虚拟人数增加
	 */
	public function activity_take_vir()
	{
		Db::startTrans();
		try {
			$total = 0;
			$now = strtotime(date("Y-m-d", time()));
			$table = Publics::instance()->tab()->take_nums;
			$query = Db::name($table)->select();
			if ($query) {
				$nums = 0;
				$total = count($query);
				foreach ($query as $v) {
					$check = false;
					$temp = [];
					$check = $v["next_time"] ? ($now == $v["next_time"]  ? true :  false) : true;
					if ($check) {
						$temp["change_time"] = $v["next_time"] ?: $v["change_time"];
						$temp["next_time"] = strtotime(date("Y-m-d", strtotime("+1 day", $temp["change_time"])));
						$temp["nums"] = Db::raw("nums + " . ($v["status"] == 2 ? rand(5, 10) : rand(10, 20)));
						$ret = Db::name($table)->where("id", $v["id"])->update($temp);
						if ($ret) {
							$nums++;
						}
					}
				}
				if ($total == $nums) {
					Db::commit();
				}
			}
		} catch (\Exception $e) {
			Db::rollback();
		}
		echo "运行新人礼参与达标人数增加！！！";
	}

	/**
	 * 新人注册交易达标
	 */
	public function activity_take_stands()
	{
		try {
			$file = Takes::instance()->file_path('regs');
			if (!file_exists($file)) {
				Takes::instance()->temyStore(1);
			}
			$pags = unserialize(file_get_contents($file));
			if ($pags) {
				if ($pags['page'] == $pags["total"]) {
					Takes::instance()->clearTable();
					Takes::instance()->temyStore(1);
				}
				$upPage = unserialize(file_get_contents($file));
				$p = $upPage["page"] + 1;

				$offset = ($p - 1) * $pags["limit"];
				$ret = Db::name(Publics::instance()->tab()->act_temy)->limit((int)$offset, (int)$upPage["limit"])->select();
				if ($ret) {
					foreach ($ret as $v) {
						Takes::instance()->standsCalc($v["actid"]);
					}
					$upPage["page"] = $p;
					file_put_contents($file, serialize($upPage));
					echo "新人有礼 - 交易达标 - 运行正常...................！！！";
				} else {
					throw new \Exception("暂无消息运行...................！！！");
				}
			} else {
				throw new \Exception("翻页信息有异常...................！！！");
			}
		} catch (\Exception $e) {
			echo $e->getMessage();
		}
	}
}
